package com.mytomcat.catalina;

import com.mytomcat.catalina.connector.Connector;
import com.mytomcat.catalina.container.Container;
import com.mytomcat.catalina.container.StandardContext;
import com.mytomcat.catalina.container.StandardEngine;
import com.mytomcat.catalina.container.StandardHost;
import com.mytomcat.catalina.container.StandardWrapper;
import com.mytomcat.catalina.net.NioEndpoint;
import com.mytomcat.catalina.server.Server;
import com.mytomcat.catalina.server.StandardServer;
import com.mytomcat.catalina.service.Service;
import com.mytomcat.catalina.service.StandardService;

/**
 * 启动类
 *
 * @author supu
 * @date 2019-04-22 17:01
 **/
public class Bootstrap {
    public static void main(String[] args) {
        Server server = new StandardServer();
        server.setPort(8005);

        Service service = new StandardService();
        service.setServer(server);

        ((StandardServer) server).addService(service);

        NioEndpoint nioEndpoint = new NioEndpoint();
        nioEndpoint.setAddress("localhost");
        nioEndpoint.setDomain("test");
        // 这个端口是网络监听端口
        nioEndpoint.setPort(8090);

        Connector connector = new Connector(nioEndpoint);
        connector.setPort(nioEndpoint.getPort());
        connector.setProtocol("HTTP/1.1");
        connector.setService(service);

        ((StandardService) service).addConnector(connector);

        // container
        Container standardEngine = new StandardEngine();

        Container standardHost = new StandardHost();

        Container standardContext = new StandardContext();

        Container standardWrapper = new StandardWrapper();

        standardContext.setContainer(standardWrapper);
        standardContext.addContainer(standardWrapper);
        standardHost.setContainer(standardContext);
        standardHost.addContainer(standardContext);
        standardEngine.setContainer(standardHost);
        standardEngine.addContainer(standardHost);

        service.setContainer(standardEngine);

        server.start();
    }
}
